<template>
	<view class="f30">
		<view class="">
			<view class="">
				<mg-cell cname='bt' ht="通用信息" htc='c3 f30 wei' />
				<view class="bf">
					<mg-cell hw='230' ht="每单限购" htc='f30' isr='1'>
						<number-box slot='ft' :min="0" :value='params.maxNum' :max="100"
							@change="bindChange($event,'maxNum')"></number-box>
					</mg-cell>
					<mg-cell hw='230' ht="虚拟销量" htc='f30' isr='1'>
						<number-box slot='ft' :min="0" :value='params.virtualSales' :max="10000"
							@change="bindChange($event,'virtualSales')"></number-box>
					</mg-cell>
					<mg-input @tab="toggleTab('dybq')" :value="params.dybq" isr='1' ht='打印标签' hw='200' htc='f30'
						pr='请选择' icn='f30 c3 t-r' dis='1' arrow="1" ftc='c3'></mg-input>
					<mg-input @tab="toggleTab('spbq')" :value="params.spbq" isr='1' ht='商品标签' hw='200' htc='f30'
						pr='请选择' icn='f30 c3 t-r' dis='1' arrow="1" ftc='c3'></mg-input>
				</view>
				<view class="mt20">
					<view class="foot-btnc p-r">
						<button @click="save" :disabled="loading" class="foot-btn"
							:style="{background:fColor}">提交</button>
					</view>
				</view>
				<!--  -->
				<w-picker v-if="dybqArr.length" mode="selector" @confirm="onConfirm($event,'dybq')" ref="dybq"
					:selectList="dybqArr">
				</w-picker>
				<w-picker v-if="spbqArr.length" mode="selector" @confirm="onConfirm($event,'spbq')" ref="spbq"
					:selectList="spbqArr">
				</w-picker>
			</view>
		</view>
		<mg-load v-show="showloading" type="2"></mg-load>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	import navTab from '@/components/common/functionCmp/nav-tab.vue';
	import numberBox from "@/components/form/number-box.vue"
	import steps from '@/components/third/steps.vue'
	import mgCell from '@/components/common/mg-cell.vue'
	import mgInput from '@/components/form/mg-input.vue'
	import mgPhoto from '@/components/form/mg-photo.vue'
	import mgTa from '@/components/form/mg-ta.vue'
	import mgEditor from '@/components/form/mg-editor.vue'
	import mgLabel from '@/components/form/mg-label.vue'
	import mgRadio from '@/components/form/mg-radio.vue'
	import mgCheckbox from "@/components/form/mg-checkbox.vue"
	import mgAgree from '@/components/form/mg-agree.vue'
	import wPicker from "@/components/form/w-picker.vue";
	import utils from '@/common/utils.js'
	import {
		uploadImg
	} from '@/common/wechat-util.js'
	import {
		utilMixins
	} from "@/common/util-mixins.js"
	export default {
		name: 'my-add-info',
		components: {
			navTab,
			numberBox,
			steps,
			mgCell,
			mgInput,
			mgPhoto,
			mgTa,
			mgEditor,
			mgLabel,
			mgRadio,
			mgCheckbox,
			mgAgree,
			wPicker,
		},
		data() {
			return {
				showloading: true,
				loading: false,
				dybqArr: [],
				spbqArr: [],
				params: {
					dybq: '',
					spbq: '',
				},
			}
		},
		async onLoad(options) {
			this.util.setNT('保存商品-其他信息')
			this.params = Object.assign(this.params, JSON.parse(decodeURIComponent(options.params)))
			// console.log(this.params)
			let arr = await Promise.all([this.util.request({
				'url': this.api.spdybqlb,
			}), this.util.request({
				'url': this.api.spbqlb,
			})])
			this.dybqArr = [{
				label: '无',
				value: ''
			}].concat(arr[0].data.map(v => ({
				label: v.name,
				value: v.id,
			})))
			this.spbqArr = [{
				label: '无',
				value: ''
			}].concat(arr[1].data.map(v => ({
				label: v.name,
				value: v.id,
			})))
			let obj = this.dybqArr.find(v => v.value == this.params.printLabel)
			if (obj) {
				this.params.dybq = obj.label
			}
			let obj2 = this.spbqArr.find(v => v.value == this.params.goodLabel)
			if (obj2) {
				this.params.spbq = obj2.label
			}
			this.showloading = false;
		},
		mixins: [utilMixins],
		computed: {},
		methods: {
			bindChange(e, key) {
				this.params[key] = e
			},
			toggleTab(str, v) {
				// console.log(str, v)
				if (str == 'time') this.xzsjobj = v
				this.$refs[str].show();
			},
			onConfirm(e, key) {
				console.log(e, key);
				switch (key) {
					case "dybq":
						this.params.dybq = e.result
						this.params.printLabel = e.checkArr.value
						break;
					case "spbq":
						this.params.spbq = e.result
						this.params.goodLabel = e.checkArr.value
						break;
				}
			},
			async save() {
				let arr = []
				let filterArr = ['dybq', 'spbq', 'maxNum', 'virtualSales', 'goodLabel', 'printLabel']
					.concat(arr)
				let judgeData = utils.isFailParams({
					field: this.params,
					filter: filterArr,
					tips: {
						id: '请完善商品基础信息后添加',
					}
				})
				if (!judgeData) {
					return
				} else {
					this.showloading = true;
					this.loading = true
					let params = utils.deepCopy(this.params)
					// return
					// console.log(fres, params)
					let res = await this.util.request({
						'url': this.api.spbc,
						ct: 1,
						method: 'POST',
						data: params,
					}).catch(() => {})
					this.showloading = false;
					if (!res) {
						this.loading = false
					} else {
						this.util.message('保存成功', 1, 1000)
						utils.stfn(() => {
							uni.navigateBack({
								delta: 2
							})
						})
					}
				}
			},
		},
	}
</script>

<style scoped lang="scss">
	.sxmain {
		padding-top: 96rpx;
	}

	.xxtx {
		background: #FEF9F0;
	}
</style>
